package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.VisitRegistration;
import com.zzyl.nursing.mapper.VisitRegistrationMapper;
import kotlin.jvm.internal.Lambda;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 预约登记Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-28
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper,Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约登记
     * 
     * @param id 预约登记主键
     * @return 预约登记
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询预约登记列表
     * 
     * @param reservation 预约登记
     * @return 预约登记
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约登记
     * 
     * @param reservation 预约登记
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return save(reservation)?1:0;
    }

    /**
     * 修改预约登记
     * 
     * @param reservation 预约登记
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return updateById(reservation)?1:0;
    }

    /**
     * 批量删除预约登记
     * 
     * @param ids 需要删除的预约登记主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除预约登记信息
     * 
     * @param id 预约登记主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return removeById(id)?1:0;
    }

    @Override
    public List<Reservation> selectBytype(Integer type) {
        return reservationMapper.selectBytype(type);
    }

    @Override
    public void updateReservationStatus() {
        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(Reservation::getTime, LocalDateTime.now())
                .eq(Reservation::getStatus,0);

        List<Reservation> list = list(queryWrapper);

        if(CollUtil.isEmpty(list))
        {
            return;
        }

        list.forEach(reservation ->
        {
            reservation.setStatus(3);
        });

        updateBatchById(list);

    }


    @Autowired
    private VisitRegistrationMapper visitRegistrationMapper;
    //老人姓名和家人姓名都是探访人 前端传入的 当是参观预约的时候是老人名字,当是拜访预约的时候是家人姓名
    @Override
    public void insertReservationByid(Long id) {
        Reservation reservation = reservationMapper.selectById(id);
        reservation.setStatus(1);
        reservation.setUpdateTime(DateUtils.getNowDate());
        reservationMapper.updateById(reservation);
        VisitRegistration visitRegistration = new VisitRegistration();
        visitRegistration.setVisitTime(LocalDateTime.now());
        visitRegistration.setVisitType(reservation.getType());
        visitRegistration.setVisitorName(reservation.getName());
        visitRegistration.setVisitorPhone(reservation.getMobile());
        visitRegistration.setCreateTime(DateUtils.getNowDate());
        visitRegistration.setElderName(reservation.getVisitor());
        visitRegistrationMapper.insertVisitRegistration(visitRegistration);
    }


}
